﻿using System.Net;
using System.Threading;
using my_proxy.Utils;
using System.Collections.Generic;
using System;

namespace my_proxy.RequestHandlers
{
    /// <summary>
    /// Handler all Client's requests and deliver the web site
    /// </summary>
    public class ProxyListener
    {
        //the conf that holds all the configartion data
        readonly Configuration _conf;
        //File Reader that reads and writes to the whitelist blacklist files 
        readonly FileReader _fileReader;
        //a lock that sync the writing to the files
        readonly object _fileLock = new object();

        public ProxyListener(Configuration cf, FileReader fr)
        {
            this._conf = cf;
            this._fileReader = fr;
            this._fileLock = new object();
        }
        /// <summary>
        /// the main function,creates a new listener on the given port and sends it contexts to the threads that 
        /// handle the requests
        /// </summary>
        public void HandleReq()
        {
            // Create a listener
            HttpListener listener = new HttpListener();
            // Add the prefix
            listener.Prefixes.Add("http://" + this._conf.Host + ":" + this._conf.Port + "/");
            //add the function that will check if we need to a Authenticate the request
            listener.AuthenticationSchemeSelectorDelegate = this.AuthenticationSchemeForClient;
            //start the listener
            listener.Start();
            while (true)
            {
                // Specify the authentication delegate.
                HttpListenerContext context = listener.GetContext();
                OpenRequestHandler requestHandler;
                

                //choose the state of the proxy - Open / Anonymous
                switch (this._conf.Mode)
                {
                    case 0:
                        requestHandler = new AnonymousRequestHandler(this._conf, context, this._fileReader, this._fileLock);
                        break;
                    
                    default:
                        requestHandler = new OpenRequestHandler(this._conf, context, this._fileReader, this._fileLock);
                        break;

                        
                }
            
                new Thread(requestHandler.ProcessRequest).Start();
            }


            
            //readr the txt blacklist
            FileReader reader = new FileReader();
            List<string> ReadBlacklist = new List<string>();
            ReadBlacklist = reader.ReadList("blacklist.txt");

            // list after enc 
            List<string> Enc_Blacklist = new List<string>();

            foreach (string line in ReadBlacklist)
            {
                string str = TripleDES.Encrypt(line);
                Enc_Blacklist.Add(str);
            }

            if (Enc_Blacklist.Contains(listener.GetContext().Request.Url.AbsolutePath))
            {
                throw new Exception();
            }

         

        }

        

        /// <summary>
        /// decides if to ask for Authentication from the user by checking the whitlist
        /// </summary>
        /// <param name="request"></param> the given request
        /// <returns></returns>
        private AuthenticationSchemes AuthenticationSchemeForClient(HttpListenerRequest request)
        {
                return AuthenticationSchemes.Anonymous;
        }

    }
}
